﻿using System;
using System.Xml;

namespace Devv.FreshTracker.Entity
{
	[Serializable]
	public class Client
	{
		#region Properties

		public Int64 Id { get; set; }
		public String Organization { get; set; }
		public String FirstName { get; set; }
		public String LastName { get; set; }
		public String Email { get; set; }

		public String DisplayMember
		{
			get { return ToString(); }
		}

		#endregion

		#region Constructor

		public Client()
		{
		}

		public Client(XmlNode node)
		{
			XmlNamespaceManager ns = new XmlNamespaceManager(node.OwnerDocument.NameTable);
			ns.AddNamespace("ns", FreshBooks.DEFAULT_XMLNS);

			Id = Convert.ToInt64(node.SelectSingleNode("ns:client_id", ns).InnerText);
			Organization = node.SelectSingleNode("ns:organization", ns).InnerText;
			FirstName = node.SelectSingleNode("ns:first_name", ns).InnerText;
			LastName = node.SelectSingleNode("ns:last_name", ns).InnerText;
			Email = node.SelectSingleNode("ns:email", ns).InnerText;
		}

		public static Client Internal()
		{
			Client c = new Client();

			c.Id = 0;
			c.Organization = Messages.Internal;

			return c;
		}

		#endregion

		#region ToString

		public override String ToString()
		{
			if (!String.IsNullOrEmpty(Organization))
			{
				return Organization;
			}

			return FirstName + " " + LastName;
		}

		#endregion
	}
}